package com.example.model.title.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.db.DBSql;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.model.title.model.ChannelModel;
import com.example.model.title.model.TitleModel;
import com.example.model.title.model.TitleModelExt;
import com.example.model.title.service.ChannelModelService;
import com.example.model.title.service.TitleModelService;
import com.example.utils.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname TitleModelServiceImpl
 * @Description TODO
 * @Date 2022/8/7 1:05
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class TitleModelServiceImpl implements TitleModelService {

    @Autowired
    DBSql dbSql;

    private static final String TITLE_TABLE_NAME=" select a.id,a.title,a.contentid,a.tag,a.ismarker,a.channelId,b.channelName,a.uid,c.realname,a.createdate from " +
            " sys_title a left join sys_channel b on a.channelId=b.id left join sys_user c on a.uid=c.id ";

    private static final String TITLE_CONTENT_TABLE_NAME=" select a.id,a.title,a.contentid,a.tag,a.ismarker,a.channelId,b.channelName,a.uid,c.realname,a.createdate,d.content " +
            " from sys_title a left join sys_channel b on a.channelId=b.id left join sys_user c on a.uid=c.id left join sys_content d on a.contentid=d.id ";

    @Autowired
    TableService tableService;

    @Autowired
    ChannelModelService channelModelService;

    @Override
    public IPage<TitleModelExt> page(Integer pageNumber, Integer pageSize, JSONObject obj) {
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.select("*").from("("+TITLE_TABLE_NAME+") a");
        if(obj != null){
            Object keyword=obj.get("keyword");
            if(!StringUtils.isEmpty(keyword)){
                sqlBuilder.and().addMark("(");
                    sqlBuilder.or("title").like(keyword);
                    sqlBuilder.or("realname").like(keyword);
                    sqlBuilder.or("channelName").like(keyword);
                    sqlBuilder.or("tag").like(keyword);
                sqlBuilder.addMark(")");
            }
            Object title=obj.get("title");
            if(!StringUtils.isEmpty(title)){
                sqlBuilder.and("title").like(title);
            }
            Object channelId=obj.get("channelId");
            if(!StringUtils.isEmpty(channelId)){
                System.out.println(channelId+" ids");
                if(!String.valueOf(channelId).equals("-1")){
                    List<ChannelModel> channelModels=channelModelService.channelModelchildList(channelId);

                    List<String> strings=channelModels.stream().map(e->{
                        return String.valueOf(e.getId());
                    }).collect(Collectors.toList());
                    strings.add(String.valueOf(channelId));
                    sqlBuilder.and("channelId").in(strings);

                }
            }
            Object realname=obj.get("realname");
            if (!StringUtils.isEmpty(realname)) {
                sqlBuilder.and("realname").like(realname);
            }
            Object tag=obj.get("tag");
            if (!StringUtils.isEmpty(tag)) {
                sqlBuilder.and("tag").like(tag);
            }
        }
        sqlBuilder.orderByDesc("createdate");
        sqlBuilder.page(pageNumber,pageSize);
        String pageSql=sqlBuilder.getPage();
        String countSql=sqlBuilder.getCountSql();
        List<TitleModelExt> list=dbSql.selectList(pageSql,TitleModelExt.class);
        int count=dbSql.selectCount(countSql);
        IPage<TitleModelExt> titleModelExtIPage=new IPage<>(count,list,pageSize,pageNumber);
        return titleModelExtIPage;
    }

    @Override
    public TitleModel save(TitleModel titleModel) {
        tableService.save(titleModel,TitleModel.class);
        return titleModel;
    }

    @Override
    public TitleModel update(TitleModel titleModel) {
        tableService.updateById(titleModel,TitleModel.class);
        return titleModel;
    }

    @Override
    public TitleModel getId(Object id) {
        TitleModel titleModel=tableService.getOneById(id,TitleModel.class);
        return titleModel;
    }

    @Override
    public TitleModelExt getTitleModelExtById(Object id) {
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.select("*").from("("+TITLE_CONTENT_TABLE_NAME+") a");
        sqlBuilder.and("id").eq(id);
        TitleModelExt titleModelExt=dbSql.selectMapObj(sqlBuilder.getSql(),TitleModelExt.class);
        return titleModelExt;
    }
}
